<?php

class MqrController extends Controller
{
	/**
	 * http://yamii.fi/mqr/10000
	 * Record the download action to DB.
	 * @param int $id the merchant id
	 */
	public function actionView($id)
	{
		//check the client type
		$detect = new Mobile_Detect();
		if ($detect->isIOS()){
			$deviceType = 'iphone';
		} elseif ($detect->isAndroidOS()) {
			$deviceType = 'android';
		} elseif ($detect->isMobile()) {
			$deviceType = 'unknown mobile';
		} elseif ($detect->isTablet()) {
			$deviceType = 'unknown tablet';
		} else {
			$deviceType = 'web';
		}
		
		$download = false;
		if ($deviceType == 'iphone' or $deviceType == 'ios') {
			$url = 'https://itunes.apple.com/us/app/yamii/id594294678';
			Yii::app()->request->redirect($url, false);
			$download = true;
		} elseif ($deviceType == 'android') {
			$url = 'https://play.google.com/store/apps/details?id=com.yamii.android';
			Yii::app()->request->redirect($url, false);
			$download = true;
		}

		BUtils::Logging('download', array(
			'id_merchant' => $id,
			'device' => $deviceType,
			'agent' => Yii::app()->request->getUserAgent(),
		));
		
		//save to DB
		if ($download) {
			$this->_storeDownloadRef($deviceType, Yii::app()->request->getUrl(), Yii::app()->request->getUrlReferrer());
		}
	}
}